package ace.j2se.l3;

public class Thread_sync {
	private static int cnt = 0;

	static class Rab implements Runnable {
		@Override
		public void run() {
			Count c = new Count();
			System.out.println("After thread #" + (++cnt)
					+ " call get_id(),id=" + c.get_id_sync());
		}
	}
}

class Count {
	private final static Object lock = new Object();
	private static int count = 0;

	public int get_id() {
		int tmp = count;
		++tmp;
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
			// TODO: handle exception
		}
		count = tmp;
		return count;
	}

	public int get_id_sync() {
		synchronized (lock) {
			int tmp = count;
			++tmp;
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
			count = tmp;
		}
		return count;
	}
}
